﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Course_UserControls_ForumMessage : System.Web.UI.UserControl
{
    private string _UserName;
    private string _Title;
    private string _Message;
    private string _MessageDate;
    private string _Joined;
    private string _Posts;
    private int _ForumID;
    private int _ThreadID;
    private int _ParentID;
    private string _Quote;
    private int _DeleteID = -1;
    private bool _IsDeleted = false;
    public delegate void LoadMessages();
    public LoadMessages LM;

    public string UserName
    {
        get
        {
            return _UserName;
        }
        set
        {
            _UserName = value;
        }
    }

    public string Title
    {
        get
        {
            return _Title;
        }
        set
        {
            _Title = value;
        }
    }

    public string Message
    {
        get
        {
            return _Message;
        }
        set
        {
            _Message = value;
        }
    }

    public string MessageDate
    {
        get
        {
            return _MessageDate;
        }
        set
        {
            _MessageDate = value;
        }
    }

    public string Joined
    {
        get
        {
            return _Joined;
        }
        set
        {
            _Joined = value;
        }
    }

    public string Posts
    {
        get
        {
            return _Posts;
        }
        set
        {
            _Posts = value;
        }
    }

    public int ForumID
    {
        get
        {
            return _ForumID;
        }
        set
        {
            _ForumID = value;
        }
    }

    public int ThreadID
    {
        get
        {
            return _ThreadID;
        }
        set
        {
            _ThreadID = value;
        }
    }

    public int ParentID
    {
        get
        {
            return _ParentID;
        }
        set
        {
            _ParentID = value;
        }
    }

    public string Quote
    {
        get
        {
            return _Quote;
        }
        set
        {
            _Quote = "<div style='background-color: #ffffff; border: 2px double black; padding: 5px;'>" + value + "</div><br />";
        }
    }

    public int DeleteID
    {
        get
        {
            return _DeleteID;
        }
        set
        {
            _DeleteID = value;
        }
    }

    public bool IsDeleted
    {
        get
        {
            return _IsDeleted;
        }
        set
        {
            _IsDeleted = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (_IsDeleted)
        {
            pnlDeleted.Visible = true;
            pnlMessage.Visible = false;
        }
        else
        {
            pnlMessage.Visible = true;
            pnlDeleted.Visible = false;
        }
        if (_DeleteID != -1)
            lnkDelete.Text = "Delete Post";
    }

    protected void CommentPost(object sender, EventArgs e)
    {
        UpdatePanelPopupExtender.ShowAt(lnkComment);
    }

    protected void DeletePost(object sender, EventArgs e)
    {
        LMSData.Message msg = new LMSData.Message(_DeleteID);
        msg.IsDeleted = true;
        msg.Save();
        LM.Invoke();
    }

    protected void UpdatePanelPopupExtender_Close(object sender, EventArgs e)
    {
        
    }

    protected void CancelComment(object sender, EventArgs e)
    {
        UpdatePanelPopupExtender.Hide();
    }

    protected void PostMessage(object sender, EventArgs e)
    {
        int UserID = 0;
        try
        {
            UserID = Convert.ToInt32(Membership.GetUser().ProviderUserKey);
        }
        catch
        {
        }
        LMSData.Message msg = new LMSData.Message();
        if (ThreadID != -1)
        {
            msg.MessageTitle = NewTitle.Text;
            msg.UserID = Convert.ToInt32(Membership.GetUser().ProviderUserKey);
            msg.MessageX = NewMessage.Text;
            msg.ThreadID = ThreadID;
            msg.Sequence = 1;
            msg.ParentID = ParentID;
            msg.IsDeleted = false;
            msg.Save();
        }
        LM.Invoke();
    }
}
